public class Main {
    public static void main(String[] args) {
        int[] prices = new int[]{1,2,3,4,5};
        System.out.println(maxProfit(prices));
    }
    public static int maxProfit(int[] prices) {
        int profit = 0;
        int max = 0;
        boolean flg = true;
        for (int i = 0;i < prices.length-1;) {
            if(prices[i] < prices[i+1]) {
                i++;
            }
            for (int j = i; j < prices.length-1;j++) {
                if(prices[j] > prices[j+1]) {
                    max = prices[j] - prices[i];
                    i = j+1;
                }
            }
            profit += max;
        }
        return profit;
    }
}
